#ifndef XLCOMM_NET_RPCSERVER_H_
#define XLCOMM_NET_RPCSERVER_H_

#include <functional>
#include <map>

#include "xlcomm/net/rpc/rpc.h"
#include "xlcomm/net/rpc/rpc_codec.h"
#include "xlcomm/net/tcp_server.h"
#include "xlcomm/noncopyable.h"

namespace xlcomm {
namespace net {

class RpcServer : noncopyable {
 public:
  using RpcMethod =
      std::function<void(const RpcParamReader& reqReader, RpcParam& resParam)>;

  RpcServer(EventLoop* loop, const InetAddress& listen_addr);

  void Start() { server_.Start(); }

  void RegisterRpc(const std::string& service, const std::string& method,
                   const RpcMethod& rpc_method) {
    reg_methods_[service + "::" + method] = rpc_method;
  }

 private:
  void OnRpcCall(const TcpConnectionPtr& conn, RpcMeta& meta);

  TcpServer server_;
  RpcCodec codec_;
  std::map<std::string, RpcMethod> reg_methods_;
};

}  // namespace net
}  // namespace xlcomm

#endif  // XLCOMM_NET_RPCSERVER_H_